package com.su.service;

import com.su.bean.Role;
import com.su.bean.User;
import com.su.dao.RoleMapper;
import com.su.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author:宿世新
 * @Dte:2022/4/8
 */
public class UserService {
    public List<Role> showInfo() {
        RoleMapper roleMapper = init02();
        return roleMapper.userJoinUserRoleJoinRoleSQL02();
    }

    public void deleteUserById(int userId) {
        UserMapper userMapper = init();
        userMapper.deleteUserInfoById(userId);
    }

    public void updateUserInfo(User user) {
        UserMapper userMapper = init();
        userMapper.updateUserInfo(user);
    }

    public User getUserInfoById(int id) {
        UserMapper userMapper = init();
        User userInfoById = userMapper.getUserInfoById(id);
        return userInfoById;
    }

    public void insertUserInfo(User user) throws Exception {
        String resource = "sqlMapConfig.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.insertUserInfo(user);
    }

    public List<User> getUserInfo() throws SQLException, IOException {
        String resource = "sqlMapConfig.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getAllUserInfo();
        return userList;
    }

    public UserMapper init() {
        String resource = "sqlMapConfig.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        return userMapper;
    }

    public RoleMapper init02() {
        String resource = "sqlMapConfig.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        return roleMapper;
    }

    @Test
    public void test01() throws Exception {
        String resource = "sqlMapConfig.xml";
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(resource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userInfoList = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userInfoList.getAllUserInfo();
        for (User info : userList) {
            System.out.println(info);
        }
    }
}
